﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Common
{
    /// <summary>
    /// 枚举帮助类
    /// </summary>
    public class EnumHelper
    {
        private static string HashKey = "EnumHelper_";
        private static System.Collections.Hashtable enumHashTable = new System.Collections.Hashtable();
        /// <summary>
        /// 获取枚举类型集合
        /// </summary>
        /// <param name="enumType">枚举类型</param>
        /// <returns></returns>
        public static IList<EnumRemarkModel> GetEnumRemarkModelList(Type enumType)
        {
            if (!enumType.IsEnum) return null;
            string key = HashKey + enumType.FullName + "_GetEnumRemarkModelList_All";
            if (null != enumHashTable[key])
                return (IList<EnumRemarkModel>)enumHashTable[key];
            IList<EnumRemarkModel> rainSelectItemButtonList = new List<EnumRemarkModel>();
            foreach (int value in Enum.GetValues(enumType))
            {
                EnumRemarkModel rrib = new EnumRemarkModel();
                System.Reflection.FieldInfo fd = enumType.GetField(Enum.GetName(enumType, value));
                if (fd != null)
                {
                    object[] attrs = fd.GetCustomAttributes(typeof(EnumRemarkAttribute), false);
                    foreach (var attr in attrs)
                    {
                        if (attr is EnumRemarkAttribute)
                        {
                            EnumRemarkAttribute erAttr = (EnumRemarkAttribute)attr;
                            rrib = erAttr.model;
                            rainSelectItemButtonList.Add(rrib);
                        }
                    }
                }
            }
            enumHashTable[key] = rainSelectItemButtonList.OrderBy(p => p.Order).ToList();
            return (IList<EnumRemarkModel>)enumHashTable[key];
        }
        /// <summary>
        /// 获取忽略项枚举类型集合
        /// </summary>
        /// <param name="enumType">枚举类型</param>
        /// <param name="ignoreItem">忽略指定类型枚举</param>
        /// <returns></returns>
        public static IList<EnumRemarkModel> GetIgnoreItemEnumRemarkModelList(Type enumType, string[] ignoreItem)
        {
            if (!enumType.IsEnum) return null;
            IList<EnumRemarkModel> rainSelectItemButtonList = new List<EnumRemarkModel>();
            IList<EnumRemarkModel> allEnumRemarkModelList = GetEnumRemarkModelList(enumType);
            foreach (var model in allEnumRemarkModelList)
            {
                if (!ignoreItem.Contains(model.Value))
                {
                    rainSelectItemButtonList.Add(model);
                }
            }
            return rainSelectItemButtonList;
        }
        /// <summary>
        /// 获取指定项枚举类型集合
        /// </summary>
        /// <param name="enumType">枚举类型</param>
        /// <param name="containItem">包含的枚举项</param>
        /// <returns></returns>
        public static IList<EnumRemarkModel> GetContainItemEnumRemarkModelList(Type enumType, string[] containItem)
        {
            if (!enumType.IsEnum) return null;
            IList<EnumRemarkModel> rainSelectItemButtonList = new List<EnumRemarkModel>();
            IList<EnumRemarkModel> allEnumRemarkModelList = GetEnumRemarkModelList(enumType);
            foreach (var model in allEnumRemarkModelList)
            {
                if (containItem.Contains(model.Value))
                {
                    rainSelectItemButtonList.Add(model);
                }
            }
            return rainSelectItemButtonList;
        }
        /// <summary>
        /// 获取枚举值的文本
        /// </summary>
        /// <param name="remarkList">枚举备注对象集合</param>
        /// <param name="enumValue">枚举值</param>
        /// <returns></returns>
        public static string GetEnumRemarkText(IList<EnumRemarkModel> remarkList, int enumValue)
        {
            if (null == remarkList || remarkList.Count() < 1) return "";
            var model = (from list in remarkList where list.Value == enumValue.ToString() select list).FirstOrDefault();
            if (null == model) return "";
            return model.Text;
        }
    }
    /// <summary>
    /// 枚举标注
    /// </summary>
    public class EnumRemarkAttribute : Attribute
    {
        public EnumRemarkModel model { get; set; }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="remark"></param>
        /// <param name="value"></param>
        /// <param name="order"></param>
        public EnumRemarkAttribute(string Text, string Value, int Order)
        {
            this.model = new EnumRemarkModel();
            model.Text = Text;
            model.Value = Value;
            model.Order = Order;
        }
    }

    /// <summary>
    /// 枚举备注对象
    /// </summary>
    public class EnumRemarkModel
    {
        /// <summary>
        /// 显示文本
        /// </summary>
        public string Text { get; set; }

        /// <summary>
        /// Radion值
        /// </summary>
        public string Value { get; set; }

        /// <summary>
        /// 序号
        /// </summary>
        public int Order { get; set; }
    }
}
